package config

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type DbConfig struct {
	Host       string
	Port       int
	DriverName string
	User       string
	Password   string
	DbName     string
}

const (
	dbUser     string = "root"
	dbPassword string = "test123456"
	dbHost     string = "localhost"
	dbPort     int    = 3306
	dbName     string = "beego"
	dbDriver   string = "mysql"
)

func init() {
	dbConfig := DbConfig{"localhost", 3306, "mysql", "root", "test123456", "beego"}
	// 注册驱动
	orm.RegisterDriver(dbConfig.DriverName, orm.DRMySQL)
	// 注册默认数据库
	// 我的mysql的root用户密码为tom，打算把数据表建立在名为test数据库里
	// 备注：此处第一个参数必须设置为“default”（因为我现在只有一个数据库），否则编译报错说：必须有一个注册DB的别名为 default
	//orm.RegisterDataBase("default", dbConfig.DriverName, "root:test123456@/beego?charset=utf8")
	//orm.RegisterDataBase("default", dbConfig.DriverName, dbConfig.User+":"+dbConfig.Password+"@/"+dbConfig.DbName+"?charset=utf8")

	ds := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&loc=Local&parseTime=true", dbUser, dbPassword, dbHost, dbPort, dbName)
	orm.RegisterDataBase("default", dbDriver, ds)
}
